<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>电脑管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/manage/layui/css/layui.css" media="all">
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
    <script type="text/javascript" src="${ctxPath}/static/page/js/jquery-1.8.3.min.js"></script>
    <script	src="${ctxPath}/static/manage/js/ajaxfileupload.js"></script>
</head>
<body>
    <div class="x-nav">
        <span class="nav-breadcrumb">
            <a><cite>后台管理></cite></a>
            <a><cite>资源管理></cite></a>
            <a><cite>资源列表</cite></a>
        </span>
    </div>
    <div class="x-body">
        <blockquote class="layui-elem-quote quoteBox">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchContent" placeholder="请输入(系列/标题)关键字" />
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-btn" id="computerAdd">
                <i class="layui-icon"></i>添加资源
            </div>

            <a href="/manage/computer/excel_down" class="layui-btn layui-btn-sm" >导出Excel</a>

            <input type="file" id="file" name="file" class="layui-btn" onchange ="uploadFile(this)"  ></input>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </blockquote>
        <table id="computerTable" lay-filter="computerTable"></table>
    </div>
</body>
</html>

<script type="text/javascript">
    function uploadFile(file){
        var index = layer.msg('正在上传，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajaxFileUpload({
            url : '/manage/computer/upload_excel', //用于文件上传的服务器端请求地址
            secureuri : false, //一般设置为false
            fileElementId : 'file', //文件上传空间的id属性  <input type="file" id="file" name="file" />
            type : 'post',
            dataType : 'text', //返回值类型 一般设置为
            success : function(result) //服务器成功响应处理函数
            {
                var result = $.parseJSON(result.replace(/<.*?>/ig,""));
                alert(result.msg);
                layer.closeAll();

            },
            error : function(result)//服务器响应失败处理函数
            {
            }
        });
        return false;
    }
    layui.use(['jquery', 'table', 'layer', 'util'], function(){
        var $ = layui.$, table = layui.table, layer = layui.layer, util = layui.util;
        var active = {
            reload: function(){
                table.reload('search_table_id', {
                    where: {
                        title: $('.searchContent').val(),
                        item:$('.searchContent').val(),
                        page: 1
                    }
                });
            }
        };


        // 搜索（利用定义的active重新载入表格）
        $('.search_btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.render({
            elem: '#computerTable'
            ,id: 'search_table_id'
            ,height: 600
            ,url: '/manage/computer/computerTable'
            ,page: true
            ,limit: 10
            ,limits : [10]
            ,cols: [[
                { checkbox: true, fixed: true },
                { field: 'id', title: '序号', width:'6%' ,sort: true},
                { field: 'type', title: '分类', width:'6%', sort: true },
                { field: 'item', title: '系列', width:'7%' },
                { field: 'title', title: '标题',width:'10%'},
                { field: 'point', title: '卖点',width:'10%' },
                { field: 'price', title: '单价',width:'5%' ,sort: true },
                { field: 'num', title: '库存',width:'5%'},
                { field: 'pictureUrl', title: '图片', width:150, templet:'<div><img src="http://{{d.pictureUrl}}"></div>'},
                { field: 'createdUser', title: '创建人',width:'10%'},
                { field: 'createdTime', title: '上传时间',width:'8%',templet :function (d){return util.toDateString(d.createdTime);}},
                { field: 'modifiedTime', title: '最后修改时间',width:'8%',templet :function (d){return util.toDateString(d.modifiedTime);}},
                { field: 'status', title: '是否上架', width:'6%', event: 'status', templet: function(d){
                        var html = ['<input type="checkbox" name="status" lay-skin="switch" lay-text="是|否"'];
                        html.push(d.status == 1 ? ' checked' : '');
                        html.push('/>');
                        return html.join('');
                    }
                },
                { field: 'tempId', title: '操作', width:'15%', templet: function(d){
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-edit"></i>编辑</a>' +
                            '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id="'+ d.tempId +'"><i class="layui-icon layui-icon-delete"></i>移除</a>';
                    }
                }
            ]]
        });

        $('#computerAdd').on('click', function(){
            layer.open({
                title : "添加",
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['800px', '550px'],
                content: "/manage/computer/toComputerAdd",
                success: function (layero, index) {
                }
            });
        });

        // 编辑
        function editComputer(rowData) {
            layer.open({
                title : "编辑" + rowData.title,
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['500px', '500px'],
                content: "/manage/computer/toComputerUpdate/" + rowData.id,
                success: function (layero, index) {

                }
            });
        }

        // 监听工具条
        table.on('tool(computerTable)', function (obj) {
            var data = obj.data, event = obj.event, tr = obj.tr;
            switch(event){
                case "edit":
                    editComputer(data);
                    break;
                case "status":
                    var statusValue = tr.find("input[name='status']").prop('checked') ? 1 : 2;
                    $.ajax({
                        type: 'post',
                        url: '/manage/computer/updateComputerStatus',
                        data: {'id': data.id, 'status':statusValue},
                        success:function(result){
                            alert(result);
                            location.replace(location.href);                // 刷新
                        }
                    });
                    break;
                case "del":
                    layer.confirm('确定要删除吗？', function(index){
                        layer.close(index);
                        $.ajax({
                            type: 'post',
                            url: '/manage/computer/deleteComputer',
                            data: {'id': data.id},
                            success:function(result){
                                alert(result);
                                window.location.reload();      // 刷新
                            }
                        });
                    });
                    break;
            }
        });
    });

</script>
